package com.egd.imapp.contoller.activity.contact;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.egd.imapp.R;
import com.egd.imapp.contoller.adapter.PickContactAdapter;
import com.egd.imapp.model.Model;
import com.egd.imapp.model.bean.PickContactInfo;
import com.egd.imapp.model.bean.UserInfo;
import com.egd.imapp.model.db.DBManager;
import com.egd.imapp.utils.Constant;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;

import java.util.ArrayList;
import java.util.List;

public class PickContactActivity extends AppCompatActivity {
    private ImageView iv_pick_contacts_back;
    private TextView tv_pick_contacts_save;
    private ListView lv_pick_contacts;
    private PickContactAdapter pickContactAdapter;
    private ArrayList<PickContactInfo> mPicks;
    private List<String> mExistMembers;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_contacts);

        // 获取传递过来的数据
        getData();

        initView();
        
        initData();

        initListener();
    }

    /**
     * 从群详情页面，携带已经存在的群成员，跳转到该页面
     *  对于已在群中的朋友，其checkbox 为选择状态，且不可更改状态
     */
    private void getData() {
        String groupId = getIntent().getStringExtra(Constant.GROUP_ID);

        if (groupId != null) {
            EMGroup group = EMClient.getInstance().groupManager().getGroup(groupId);
            // 获取群中已经存在的所有群成员
            mExistMembers = group.getMembers();
            mExistMembers.add(group.getOwner());
        }

        if (mExistMembers == null) {
            mExistMembers = new ArrayList<>();
        }
    }

    private void initListener() {
        // 返回
        iv_pick_contacts_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        // listView条目的点击事件
        lv_pick_contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                // checkbox的切换
                CheckBox cb_pick = view.findViewById(R.id.cb_item_pick_contacts);
                cb_pick.setChecked( !cb_pick.isChecked());

                // 修改数据
                PickContactInfo pickContactInfo = mPicks.get(position);
                pickContactInfo.setIsChecked(cb_pick.isChecked());

                // 刷新页面
                pickContactAdapter.notifyDataSetChanged();
            }
        });

        // 保存按钮的点击事件
        tv_pick_contacts_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取到已经选择的联系人
                List<String> names = pickContactAdapter.getPickContacts();

                // 给启动页面返回数据
                Intent intent = new Intent();
                intent.putExtra("members", names.toArray(new String[names.size()]));

                setResult(RESULT_OK, intent);

                // 结束当前页面
                finish();

            }
        });
    }

    private void initData() {

        // 从本地数据库中获取所有联系人信息
        DBManager dbManager = getDBManger();
        if (dbManager == null){
            return;
        }

        List<UserInfo> contacts = dbManager.getContactTableDao().getContacts();

        mPicks = new ArrayList<>();

        if (contacts != null && contacts.size() >= 0){

            for (UserInfo userInfo : contacts){
                PickContactInfo pickContactInfo = new PickContactInfo(userInfo, false);
                mPicks.add(pickContactInfo);

            }
        }

        pickContactAdapter = new PickContactAdapter(this, mPicks, mExistMembers);

        lv_pick_contacts.setAdapter(pickContactAdapter);
    }



    private void initView() {
        iv_pick_contacts_back = findViewById(R.id.iv_pick_contacts_back);
        tv_pick_contacts_save = findViewById(R.id.tv_pick_contacts_save);
        lv_pick_contacts = findViewById(R.id.lv_pick_contacts);
        
    }
    private DBManager getDBManger() {
        return Model.getInstance().getDbManager();
    }

}